package com.gaojiawei.multiThread06_ThreeSpecialVisibility;

import org.junit.Test;

/**
 * 使用Synchronize解决可见性的问题
 * Synchronize里的变量，操作时会先将内部涉及到的变量从cpu缓存当中移除，从主存当中获取，修改之后释放锁的时候把变量同步到主内存当中
 */
public class l03_ResolveVisibilityWithSynchronize {

    private boolean flag = true;

    @Test
    public void testSynchronize() throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (flag) {
                synchronized (l03_ResolveVisibilityWithSynchronize.class){

                }

            }
            System.out.println("线程被终止");
        });
        thread.start();

        Thread.sleep(1000);

        flag = false;

        Thread.sleep(1000);
    }
}
